<template>
  <div>
    <!--面包屑导航-->
    <Breadcrumb></Breadcrumb>
    <!--1， 产品搜索-->
    <div class="header">
      <div class="form">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="产品名称">
            <el-input
              v-model="formInline.name"
              placeholder="产品名称"
              size="small"
            ></el-input>
          </el-form-item>
          <el-form-item label="添加时间">
            <el-date-picker
              v-model="formInline.date"
              type="date"
              placeholder="选择日期"
              size="small"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small" @click="onSubmit"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="group">
        <el-button
          type="warning"
          size="small"
          icon="el-icon-plus"
          @click="toProductPage"
          >添加商品</el-button
        >
        <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          @click="deleteAll"
          >批量删除</el-button
        >
      </div>
    </div>
    <!-- 2，产品 列表-->
    <div class="content">
      <el-table
        :data="tableData"
        stripe
        border
        style="width: 100%"
        header-row-class-name="active-header"
        cell-class-name="table-center"
        @select="selectHandler"
        @select-all="selectHandler"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column
          prop="id"
          label="商品编号"
          sortable
          width="120"
          header-align="center"
        >
        </el-table-column>
        <el-table-column
          prop="title"
          label="商品名称"
          width="100"
          header-align="center"
        >
          <template slot-scope="scope">
            <span
              style="cursor: pointer; color: blue"
              @click="handleDetail(scope.$index, scope.row)"
              >{{ scope.row.title }}</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="price" label="商品价格" header-align="center">
        </el-table-column>
        <el-table-column prop="category" label="商品类目" header-align="center">
        </el-table-column>
        <el-table-column label="添加时间" width="200" header-align="center">
          <template slot-scope="scope">
            {{ moment(scope.row.create_time).format("YYYY-MM-DD HH:mm:SS") }}
          </template>
        </el-table-column>
        <el-table-column
          prop="sellPoint"
          label="商品卖点"
          show-overflow-tooltip
          header-align="center"
        >
        </el-table-column>
        <el-table-column
          label="商品描述"
          show-overflow-tooltip
          header-align="center"
        >
          <template slot-scope="scope">
            <div>{{ removeHTMLTag(scope.row.descs) }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" header-align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)"
              icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              icon="el-icon-delete"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        :pageSize="pageSize"
        @CurrentChange="CurrentChange"
      ></Pagination>
    </div>
  </div>
</template>

<script>
import Pagination from "@/components/pagination/Pagination.vue";
import moment from "moment";
import { removeHTMLTag } from "@/utils/common.js";
import { mapMutations, mapState } from "vuex";
export default {
  components: {
    Pagination,
  },

  data() {
    return {
      formInline: {
        name: "",
        date: "",
      },

      tableData: [],
      total: 10,
      pageSize: 1,
      currentPage: 1,
      ids: [], //存储当前选中的id
    };
  },
  methods: {
    //查看商品详情操作
    handleDetail(index, row) {
      this.changeRowData(row);
      this.changeTitle("详情");
      //跳转页面
      this.$router.push("/product/add-product");
    },
    //点击勾选选择框, 选中的时候可以获取当前行
    selectHandler(selection) {
      console.log("selection", selection); //selection是数组
      let arr = [];
      selection.forEach((element) => {
        arr.push(element.id);
      });
      this.ids = arr;
    },
    async deleteAll() {
      //接口要的是字符串， 数组->字符串
      let idStr = this.ids.join(",");
      let res = await this.$api.batchDelete({ ids: idStr });
      if (res.data.status === 200) {
        this.projectList(1);
      }
    },
    removeHTMLTag,
    moment,
    //点击添加到跳转页面
    toProductPage() {
      this.changeTitle("添加");
      this.$router.push("/product/add-product");
    },

    onSubmit() {
      console.log("submit!", this.formInline);
      this.search(this.formInline.name);
    },
    ...mapMutations("product", ["changeRowData", "changeTitle"]),
    handleEdit(index, row) {
      console.log(index, row);
      //存储当前行的数据到vuex
      //跳转编辑界面, 让编辑页面可以从vuex中获取传递过去的商品数据
      this.changeRowData(row);
      this.changeTitle("编辑");
      this.$router.push("/product/add-product");
    },
    handleDelete(index, row) {
      this.$confirm("此操作将删除商品记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //删除数据- 请求后台
          this.deleteItemById(row.id),
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          //如果该行是最后一页的最后一条记录 ,获取上一页数据
          //判断 this.total总数目
          if (this.total % this.pageSize === 1) {
            this.currentPage =
              this.currentPage - 1 > 0 ? this.currentPage - 1 : 1;
          }

          this.projectList(this.currentPage);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //获取产品列表数据
    async projectList(page) {
      let res = await this.$api.projectList({ page });
      // console.log("产品列表数据-----", res.data);
      this.tableData = res.data.data;
      this.total = res.data.total;
      this.pageSize = res.data.pageSize;
    },
    //搜索接口
    async search(search) {
      if (!search) {
        this.currentPage = 1;
        this.projectList(1);
        return;
      }
      let res = await this.$api.search({ search });
      // console.log("搜索的数据---", res.data);
      if (res.data.status === 200) {
        this.tableData = res.data.result; //有数据 渲染页面
        this.total = res.data.result.length;
        this.pageSize = res.data.result.length;
      } else {
        this.tableData = []; //无数据
        this.total = 1;
        this.pageSize = 1;
      }
    },
    //根据id删除商品接口
    async deleteItemById(id) {
      let res = await this.$api.deleteItemById({ id });
      // console.log("删除----", res.data);
    },

    CurrentChange(val) {
      // console.log("----子组件pagination传递过来的当前页码参数:", val);
      this.currentPage = val;
      //向服务器请求传递过来的页码的数据
      this.projectList(val);
    },
  },
  created() {
    this.projectList(1);
    // console.log("获取当前时间- 默认格式", new Date());
    // console.log(
    //   "获取当前时间- momentJS处理后的格式",
    //   moment(new Date()).format("YYYY-MM-DD HH:mm:SS")
    // );
  },
};
</script>

<style lang="less" scoped>
.header {
  background: #fff;
  margin-bottom: 20px;
  padding: 10px;
  .el-form-item {
    margin-bottom: 10px;
  }
  .group {
    border: 1px solid #eee;
    padding: 5px;
  }
}
.content {
  background: #fff;
  /deep/ .active-header {
    text-align: center;
    color: #333;
  }

  /deep/ .table-center {
    text-align: center;
  }
}
</style>